﻿using System;
using System.Web.Mvc;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;
using iss.sg4210.se18.team6s.Views.Utility;
using System.Web;
using System.Linq;

namespace iss.sg4210.se18.team6s.Controllers
{
    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            string[] users = Users.Split(',');
            string[] roles = Roles.Split(',');

            if (!httpContext.User.Identity.IsAuthenticated)
                return false;

            if (users.Length > 0 && !users.Contains(httpContext.User.Identity.Name, StringComparer.OrdinalIgnoreCase))
                return false;

            return true;
        }
    }

    public class AdvertisementWizardController : Controller
    {
        //    public ActionResult Create()
        //    {
        //        return View("Advertisement");
        //    }

        //    public ActionResult CreateAdvertisementSkill()
        //    {
        //        return View("AdvertisementSkill");
        //    }

        //    //
        //    // POST: /Advertisement/Create
        //    [HttpPost]
        //    public ActionResult Advertisement(FormCollection collection)
        //    {
        //        try
        //        {
        //            // TODO: Add insert logic here
        //            Advertisement _advertisement = new Advertisement();
        //            UpdateModel(_advertisement, collection);
        //            _advertisement.UserId = Common.GetLoggedInUserId();
        //            Session["Advertisement"] = _advertisement;

        //            return RedirectToAction("CreateAdvertisementSkill");
        //        }
        //        catch (Exception ex)
        //        {
        //            ModelState.AddModelError(string.Empty, ex.Message); return View("Advertisement");
        //        }
        //    }

        //    [HttpPost]
        //    public ActionResult CreateAdvertisementSkill(FormCollection collection)
        //    {
        //        try
        //        {
        //            // TODO: Add insert logic here
        //            AdvertisementSkill _advertisementSkill = new AdvertisementSkill();
        //            UpdateModel(_advertisementSkill, collection);

        //            Advertisement _advertisement = (Advertisement)Session["Advertisement"];
        //            DataAccessFactory.Instance.Advertisement.AddAdvertisement(_advertisement);
        //            DataAccessFactory.Instance.AdvertisementSkill.AddAdvertisementSkill(_advertisementSkill);

        //            return RedirectToAction("Index");
        //        }
        //        catch (Exception ex)
        //        {
        //            ModelState.AddModelError(string.Empty, ex.Message); return View("AdvertisementSkill");
        //        }
        //}
    }

    [Authorize()]
    public class AdvertisementController : AdvertisementWizardController
    {
        //
        // GET: /Advertisement/

        public ActionResult Index()
        {
            return View(DataAccessFactory.Instance.Advertisement.FindAllAdvertisementsByUser(Common.GetLoggedInUserId()));
        }

        //
        // GET: /Advertisement/Details/5

        public ActionResult Details(int id)
        {
            Advertisement advertisement = DataAccessFactory.Instance.Advertisement.GetAdvertisement(id);
            return View(advertisement);
        }

        //
        // GET: /Advertisement/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Advertisement/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Advertisement advertisement = new Advertisement();
                UpdateModel(advertisement, collection);
                advertisement.UserId = Common.GetLoggedInUserId();
                DataAccessFactory.Instance.Advertisement.AddAdvertisement(advertisement);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }

        //
        // GET: /Advertisement/Edit/5

        public ActionResult Edit(int id)
        {
            Advertisement advertisement = DataAccessFactory.Instance.Advertisement.GetAdvertisement(id);
            return View(advertisement);
        }

        //
        // POST: /Advertisement/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                Advertisement advertisement = DataAccessFactory.Instance.Advertisement.GetAdvertisement(id);
                UpdateModel(advertisement);
                DataAccessFactory.Instance.Advertisement.UpdateAdvertisement(advertisement);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }

        //
        // GET: /Advertisement/Delete/5

        public ActionResult Delete(int id)
        {
            Advertisement advertisement = DataAccessFactory.Instance.Advertisement.GetAdvertisement(id);
            return View(advertisement);
        }

        //
        // POST: /Advertisement/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                DataAccessFactory.Instance.Advertisement.DeleteAdvertisement(id);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }

        public ActionResult AdvertisementsCompanyList(int id)
        {
            if (id < 1)
                ViewData["CompanyList"] = new SelectList(DataAccessFactory.Instance.Company.FindAllCompanies(), "Id", "Name");
            else
                ViewData["CompanyList"] = new SelectList(DataAccessFactory.Instance.Company.FindAllCompanies(), "Id", "Name", DataAccessFactory.Instance.Advertisement.GetCompanyByAdvertisementId(id).Id);

            return View("CompanyDropDownList");
        }

        public ActionResult AdvertisementsGeoAreaList(int id)
        {
            if (id < 1)
                ViewData["GeoAreaList"] = new SelectList(DataAccessFactory.Instance.GeographicalArea.FindAllGeographicalAreas(), "Id", "Name");
            else
                ViewData["GeoAreaList"] = new SelectList(DataAccessFactory.Instance.GeographicalArea.FindAllGeographicalAreas(), "Id", "Name", DataAccessFactory.Instance.Advertisement.GetGeographicalAreaByAdvertisementId(id).Id);

            return View("GeoAreaDropDownList");
        }

        public ActionResult AdvertisementsAdvertisementTypeList(int id)
        {
            if (id < 1)
                ViewData["AdvertisementTypeList"] = new SelectList(DataAccessFactory.Instance.AdvertisementType.FindAllAdvertisementTypes(), "Id", "Type");
            else
                ViewData["AdvertisementTypeList"] = new SelectList(DataAccessFactory.Instance.AdvertisementType.FindAllAdvertisementTypes(), "Id", "Type", DataAccessFactory.Instance.Advertisement.GetAdvertisementTypeByAdvertisementId(id).Id);

            return View("AdvertisementTypeDropDownList");
        }
    }

    [Authorize()]
    public class AdvertisementTypeController : AdvertisementWizardController
    {
        //
        // GET: /AdvertisementType/

        public ActionResult Index()
        {
            return View(DataAccessFactory.Instance.AdvertisementType.FindAllAdvertisementTypes());
        }

        //
        // GET: /AdvertisementType/Details/5

        public ActionResult Details(int id)
        {
            AdvertisementType advertisementType = DataAccessFactory.Instance.AdvertisementType.GetAdvertisementType(id);
            return View(advertisementType);
        }

        //
        // GET: /AdvertisementType/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AdvertisementType/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                AdvertisementType advertisementType = new AdvertisementType();
                UpdateModel(advertisementType, collection);
                DataAccessFactory.Instance.AdvertisementType.AddAdvertisementType(advertisementType);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
        }

        //
        // GET: /AdvertisementType/Edit/5

        public ActionResult Edit(int id)
        {
            AdvertisementType advertisementType = DataAccessFactory.Instance.AdvertisementType.GetAdvertisementType(id);
            return View(advertisementType);
        }

        //
        // POST: /AdvertisementType/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                AdvertisementType advertisementType = DataAccessFactory.Instance.AdvertisementType.GetAdvertisementType(id);
                UpdateModel(advertisementType);
                DataAccessFactory.Instance.AdvertisementType.UpdateAdvertisementType(advertisementType);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }

        //
        // GET: /AdvertisementType/Delete/5

        public ActionResult Delete(int id)
        {
            AdvertisementType advertisementType = DataAccessFactory.Instance.AdvertisementType.GetAdvertisementType(id);
            return View(advertisementType);
        }

        //
        // POST: /AdvertisementType/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                DataAccessFactory.Instance.AdvertisementType.DeleteAdvertisementType(id);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }
    }

    [Authorize()]
    public class AdvertisementSkillsController : AdvertisementWizardController
    {
        //
        // GET: /AdvertisementSkills/

        public ActionResult Index()
        {
            return View(DataAccessFactory.Instance.AdvertisementSkill.FindAllAdvertisementSkills());
        }

        //
        // GET: /AdvertisementSkills/Details/5

        public ActionResult Details(int id)
        {
            AdvertisementSkill advertisementSkill = DataAccessFactory.Instance.AdvertisementSkill.GetAdvertisementSkill(id);
            return View(advertisementSkill);
        }

        //
        // GET: /AdvertisementSkills/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AdvertisementSkills/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                AdvertisementSkill advertisementSkill = new AdvertisementSkill();
                UpdateModel(advertisementSkill, collection);
                DataAccessFactory.Instance.AdvertisementSkill.AddAdvertisementSkill(advertisementSkill);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }

        //
        // GET: /AdvertisementSkills/Edit/5

        public ActionResult Edit(int id)
        {
            AdvertisementSkill advertisementSkill = DataAccessFactory.Instance.AdvertisementSkill.GetAdvertisementSkill(id);
            return View(advertisementSkill);
        }

        //
        // POST: /AdvertisementSkills/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                AdvertisementSkill advertisementSkill = DataAccessFactory.Instance.AdvertisementSkill.GetAdvertisementSkill(id);
                UpdateModel(advertisementSkill);
                DataAccessFactory.Instance.AdvertisementSkill.UpdateAdvertisementSkill(advertisementSkill);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }

        //
        // GET: /AdvertisementSkills/Delete/5

        public ActionResult Delete(int id)
        {
            AdvertisementSkill advertisementSkill = DataAccessFactory.Instance.AdvertisementSkill.GetAdvertisementSkill(id);
            return View(advertisementSkill);
        }

        //
        // POST: /AdvertisementSkills/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                DataAccessFactory.Instance.AdvertisementSkill.DeleteAdvertisementSkill(id);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError(string.Empty, ex.Message); 
                return View();
            }
        }
    }
}
